﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DataAccessLayer;
using Entities;
using BusinessLogicLayer;
using BusinessLogicLayer.Validation;
using QuanLyTKB_TienDoGD.In;

namespace QuanLyTKB_TienDoGD.QuanLyGiaoVien
{
    public partial class frmQuanLyGiaoVien : Office2007Form
    {
        private IRepository<GiaoVien> _repository;
        private GiaoVienManager _manager;
        public frmQuanLyGiaoVien()
        {
            InitializeComponent();
        }

        private void frmQuanLyGiaoVien_Load(object sender, EventArgs e)
        {
            _repository = new Repository<GiaoVien>();
            _manager = new GiaoVienManager(_repository);
            dgvGiaoVien.DataSource = _repository.GetAll().ToList();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            var frm = new frmAddGiaoVien(_manager, this);
            frm.Text = "Thêm giáo viên";
            frm.ShowDialog();
            dgvGiaoVien.DataSource = _repository.GetAll().ToList();
            frm.btnReset.PerformClick();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa giáo viên đã chọn ?", "Xóa giáo viên", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
                return;
            foreach (DataGridViewRow row in dgvGiaoVien.Rows)
            {
                if (row.Selected)
                {
                    var aResult = _manager.Delete(row.Cells[0].Value.ToString());
                    if (aResult.Success) MessageBox.Show("Xóa thành công");
                    else MessageBox.Show("Dữ liệu giáo viên đang sử dụng!", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            dgvGiaoVien.DataSource = _repository.GetAll().ToList();
        }

        private void btnCapnhat_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("Bạn có muốn cập nhập lại ?", "Cập nhật giáo viên", MessageBoxButtons.OKCancel,MessageBoxIcon.Warning,MessageBoxDefaultButton.Button1) == DialogResult.Cancel)
            {
                btnLamMoi.PerformClick();
                return;
            }
            if(!KiemTraSuThayDoi())
            {
                MessageBox.Show("Không có sự thay đổi!");
                return;
            }
            GiaoVien gv = new GiaoVien();
            foreach (DataGridViewRow row in dgvGiaoVien.Rows)
            {
                gv.MaGV = row.Cells[0].Value.ToString();
                gv.TenGV = row.Cells[1].Value.ToString();
                gv.NgaySinh = Convert.ToDateTime(row.Cells[2].Value.ToString());
                gv.GioiTinh = row.Cells[3].Value.ToString();
                gv.ChucVu = row.Cells[4].Value.ToString();
                gv.HocVi = row.Cells[5].Value.ToString();
                gv.DiaChi = row.Cells[6].Value.ToString();
                gv.DienThoai = row.Cells[7].Value.ToString();
                gv.Email = row.Cells[8].Value.ToString();

                var toResult = _manager.Validate(gv);
                if (toResult.Valid)
                {
                    GiaoVien.Repository.Update(gv);
                }
                else
                {
                    MessageBox.Show(toResult.ToString());
                    return;
                }
            }
            MessageBox.Show("Cập nhật hoàn tất!");
        }

        private void btnLamMoi_Click(object sender, EventArgs e)
        {
            dgvGiaoVien.DataSource = _repository.GetAll().ToList();
            
        }

        private bool KiemTraSuThayDoi()
        {
            foreach (DataGridViewRow row in dgvGiaoVien.Rows)
            {
                GiaoVien gv = new GiaoVien();
                gv = GiaoVien.Repository.GetByID(row.Cells[0].Value.ToString());

                GiaoVien gv2 = new GiaoVien();
                gv2.TenGV = row.Cells[1].Value.ToString();
                gv2.NgaySinh = Convert.ToDateTime(row.Cells[2].Value.ToString());
                gv2.GioiTinh = row.Cells[3].Value.ToString();
                gv2.ChucVu = row.Cells[4].Value.ToString();
                gv2.HocVi = row.Cells[5].Value.ToString();
                gv2.DiaChi = row.Cells[6].Value.ToString();
                gv2.DienThoai = row.Cells[7].Value.ToString();
                gv2.Email = row.Cells[8].Value.ToString();
                if (!sosanh(gv, gv2)) return true;
                
            }
            return false;
        }
        private bool sosanh(GiaoVien a, GiaoVien b)
        {
            if (a.TenGV.CompareTo(b.TenGV) != 0) return false;
            if (a.NgaySinh.CompareTo(b.NgaySinh) != 0) return false;
            if (a.GioiTinh.CompareTo(b.GioiTinh) != 0) return false;
            if (a.ChucVu.CompareTo(b.ChucVu) != 0) return false;
            if (a.HocVi.CompareTo(b.HocVi) != 0) return false;
            if (a.DiaChi.CompareTo(b.DiaChi) != 0) return false;
            if (a.DienThoai.CompareTo(b.DienThoai) != 0) return false;
            if (a.Email.CompareTo(b.Email) != 0) return false;
            return true;
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            var aResult = _manager.Find(s => s.TenGV.IndexOf(txtTimKiemGV.Text) >= 0);
            if (aResult.Success)
            {
                dgvGiaoVien.DataSource = aResult.Result;
            }
            else
            {
                MessageBox.Show(aResult.GetErrosMessages());
                btnLamMoi.PerformClick();
                txtTimKiemGV.ResetText();
                txtTimKiemGV.Focus();
            }
        }

        private void frmInGiaoVien_Click(object sender, EventArgs e)
        {

        }

        private void panelEx1_Click(object sender, EventArgs e)
        {

        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            frmInDanhSachGiaoVien frmInGV = new frmInDanhSachGiaoVien();
            frmInGV.Show();
        }

  
    }
}
